<template>
  <div class="login_body">
    <!-- 左边背景图 -->
    <div class="bg" />
    <!-- 右边登录表单 -->
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!-- elemnetui 中提供的一个组件=>表单结构
        from组件=>自带表单=>配置规则
        el-from   :model='表单数据对象'   :rules绑定的规则
        el-form-item prop='要检验的字段'
        el-input v-model="xxx"
      -->
      <el-form ref="form" :model="form" :rules="rules"><!-- ref="标识符" -->
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="form.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input type="password" v-model="form.password" /><!--数据双向绑定v-model-->
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="flag">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="login">登录</el-button><!-- ***@click点击效果 -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user';
export default {
  name: 'Login',
  //提供数据
  data(){
    return {
      // balance:null,
      flag:false,
      form:{
        username:'',
        password:'',
      },

      //提供规则
      rules:{
        //检查用户名
        username:[
          //非空校验
          {required:true,message:'用户名必须填写',trigger:'blur'},
          //长度校验
          {min:5,max:12,message:'用户名必须5-12位',tirgger:'blur'}
        ],
        //校验密码
        password:[
          //非空
          {required:true,message:'密码不能为空',tirgger:'blur'},
          //长度
          {min:8,max:12,message:'密码长度必须8-12位',tirgger:'blur'}
        ],
      },
    }
  },
  //生命周期函数=>特定时机触发
  mounted(){
    const temp=localStorage.getItem('from')
    if(temp){
      this.form.username=JSON.parse(temp).username
      this.form.password=JSON.parse(temp).password
      this.flag=true
    }
  },
  //提供方法
  methods:{
    login(){
      console.log(this.form)
      //登录请求之前需要对整个表单进行整体校验=>需要用到form组件的一个方法=>获取到from组件(添加ref)
      //this.$refs.名字=>拿到组件=》调用组件方法validate
      this.$refs.form.validate((ok)=>{
        if(ok){
          //进行登录请求
          loginAPI(this.form).then((res)=>{
            console.log(res.data.token)

            this.$store.commit('user/setToken',res.data.token)
          //跳转首页
           this.$router.push('/')
          //提示
            this.$message.success('恭喜登录成功')
          
            if(this.flag){
              localStorage.setItem('form',JSON.stringify(this.form))
            }
          })
        }
      })
    },

  },

}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
